package com.wx.mall.operate.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.SeckillActivitiesDto;
import com.wx.mall.operate.entity.SeckillActivities;
import com.wx.mall.operate.service.impl.SeckillActivitiesServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-28
 */
@RestController
@RequestMapping("/seckill/activities")
public class SeckillActivitiesController {

    @Autowired
    SeckillActivitiesServiceImpl activitiesService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/getPageList")
    public CommonResult<CommonPage<SeckillActivities>> getFindPages(SeckillActivitiesDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<SeckillActivities> list=activitiesService.findAllActivities(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "筛选查询")
    @GetMapping("/getPageAllList")
    public CommonResult<CommonPage<SeckillActivities>> getFindAllPages(SeckillActivitiesDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<SeckillActivities> list=activitiesService.findActivityList(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "添加保存")
    @PostMapping("/getAddActivity")
    public CommonResult<Integer> getAddActivityList(SeckillActivitiesDto dto){
        int i = activitiesService.addActivityList(dto);
        return i==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "编辑修改")
    @PutMapping("/getUpdateActivity")
    public CommonResult<Integer> getUpdateActivityList(SeckillActivitiesDto dto){
        int i = activitiesService.updateActivityList(dto);
        return i==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "软删除")
    @DeleteMapping("/getDeleteActivity")
    public CommonResult<Integer> getDelActivityList(Integer id){
        int i = activitiesService.delActivityList(id);
        return i==1?CommonResult.success():CommonResult.failed();
    }

}

